package dk.eclipse.android.badassapp.helper;

import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.Context;
import android.media.AudioManager;
import android.os.Environment;

public class DeviceHelper {

	/**
	 * Checks to see if the audio manager is in silent mode.
	 * 
	 * @param context
	 */
	public static boolean isSilentMode(Context context) {
		AudioManager am = (AudioManager) context.getSystemService(Activity.AUDIO_SERVICE);
		return (am.getRingerMode() == AudioManager.RINGER_MODE_SILENT);
	}

	/**
	 * Sets the audio manager's silent mode.
	 * 
	 * @param context
	 * @param silent
	 */
	public static void setSilentMode(Context context, boolean silent) {
		AudioManager am = (AudioManager) context.getSystemService(Activity.AUDIO_SERVICE);
		int mode = (silent ? AudioManager.RINGER_MODE_SILENT : AudioManager.RINGER_MODE_NORMAL); 
		am.setRingerMode(mode);
	}

	/**
	 * Checks to see if a SD card is available (mounted).
	 * 
	 * @return true if SD card is available
	 */
	public static boolean isSDAvailable() {
		
		return (Environment.getExternalStorageState().equals( 
				Environment.MEDIA_MOUNTED)); 
	}
	
	/**
	 * Locks the screen.
	 * 
	 * @param context
	 * @return the key guard lock
	 */
	public KeyguardLock lockScreen(Context context) {
		KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Activity.KEYGUARD_SERVICE); 
		KeyguardLock lock = keyguardManager.newKeyguardLock(Context.KEYGUARD_SERVICE); 
		lock.disableKeyguard(); 
		return lock;
	}
	
	/**
	 * Unlocks the screen.
	 * 
	 * @param context
	 * @return the key guard lock 
	 */
	public KeyguardLock unlockScreen(Context context) {
		KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Activity.KEYGUARD_SERVICE); 
		KeyguardLock lock = keyguardManager.newKeyguardLock(Context.KEYGUARD_SERVICE); 
		lock.reenableKeyguard(); 
		return lock;
	}

}
